/**
 * 
 * If you have edited the file, please update
 * all the last-modified fields manually. Thanks!
 *
 * @last-modified by: 
 * @last-modified date: 
 * @last-modified time: 
 *
 * @author Swee Zhi
 * @date-created Sep 26, 2011    @time-created 11:50:43 PM
 *
 * @project: 
 * @module: 
 * @institution: 
 *
 */
package util.generator;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import util.localization.MenuLocalization;

public class StakeholderMenuGenerator {

    private MenuLocalization menuLocalization;
    private MenuModel microPortalModel;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private transient FacesContext context;
    private transient UIViewRoot view;

    public StakeholderMenuGenerator() {
        microPortalModel = new DefaultMenuModel();
        menuLocalization = new MenuLocalization();
    }

      private void initContextMemebers() {
        context = FacesContext.getCurrentInstance();
        view = context.getViewRoot();
    }
    public MenuModel initView() {
        String url = getRequestURL();
        if (url.contains("Portal") || url.contains("Branch") || url.contains("Media")) {
            microPortalModel = generateMicroPortalMenu();
        } else if (url.contains("Advertisement")) {
            microPortalModel = generateAdvMenu();
        } else if (url.contains("Message")) {
            microPortalModel = generateMailBoxMenu();
        } else if (url.contains("Event")) {
            microPortalModel = generateEventMenu();
        } else {
            microPortalModel = generateAccountMenu();
        }
        return microPortalModel;
    }

    public String getRequestURL() {
        Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request instanceof HttpServletRequest) {
            return ((HttpServletRequest) request).getRequestURL().toString();
        } else {
            return "";
        }
    }

    //Generate MicroPortalMenu
    public MenuModel generateMicroPortalMenu() {
        initContextMemebers();
        MenuModel model = new DefaultMenuModel();
        if (!ctx.getExternalContext().getSessionMap().isEmpty()) {
            String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
            int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
            String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

            //---------Branch Portion-------
            Submenu submenu = new Submenu();
            submenu.setLabel(menuLocalization.getStakeholderMenuItem("BRANCH_MANAGEMENT"));
           submenu.setId(view.createUniqueId());

            MenuItem menu1 = new MenuItem();
            menu1.setId(view.createUniqueId());
            menu1.setValue(menuLocalization.getStakeholderMenuItem("ADD_BRANCH"));
            String URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/AddBranch.xhtml";
            menu1.setUrl(URL);
            submenu.getChildren().add(menu1);
            model.addSubmenu(submenu);

            MenuItem menu2 = new MenuItem();
            menu2.setValue(menuLocalization.getStakeholderMenuItem("VIEW_BRANCH"));
            menu2.setId(view.createUniqueId());
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/ViewAllBranch.xhtml";
            menu2.setUrl(URL);
            submenu.getChildren().add(menu2);
            model.addSubmenu(submenu);

            //---------Media Portion-------
            submenu = new Submenu();
            submenu.setLabel(menuLocalization.getStakeholderMenuItem("MEDIA_MANAGEMENT"));
            submenu.setId(view.createUniqueId());
            
            MenuItem menu3 = new MenuItem();
            menu3.setId(view.createUniqueId());
            menu3.setValue(menuLocalization.getStakeholderMenuItem("ADD_MEDIA"));
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/AddMedia.xhtml";
            menu3.setUrl(URL);
            submenu.getChildren().add(menu3);
            model.addSubmenu(submenu);

            MenuItem menu4 = new MenuItem();
            menu4.setId(view.createUniqueId());
            menu4.setValue(menuLocalization.getStakeholderMenuItem("VIEW_MEDIA"));
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/ViewMedia.xhtml";
            menu4.setUrl(URL);
            submenu.getChildren().add(menu4);
            model.addSubmenu(submenu);

            MenuItem menu5 = new MenuItem();
            menu5.setId(view.createUniqueId());
            menu5.setValue(menuLocalization.getStakeholderMenuItem("DELETE_MEDIA"));
            URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/RemoveMedia.xhtml";
            menu5.setUrl(URL);
            submenu.getChildren().add(menu5);
            model.addSubmenu(submenu);
        } else {
            ctx.getExternalContext().getRealPath("index.xhtml");
        }
        return model;
    }
    
    //create IOTR 
    
    public MenuModel generateIOTRMenu(){
         String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        MenuModel model = new DefaultMenuModel();

        Submenu submenu = new Submenu();submenu.setLabel("Meal Management");
        submenu.setId(view.createUniqueId());
        
        MenuItem item9 = new MenuItem();
        //item2.setId();
        item9.setValue("Add Menu");
        item9.setId(view.createUniqueId());
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderAddMeal..xhtml";
        item9.setUrl(URL);
        submenu.getChildren().add(item9);  
        
        MenuItem item8 = new MenuItem();
        item8.setValue("View Menu");
        item8.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewMeal.xhtml";
        item8.setUrl(URL);
        submenu.getChildren().add(item8);
        
        MenuItem item7 = new MenuItem();
        item7.setValue("Add a Set Meal");
        item7.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderAddSetMeal.xhtml";
        item7.setUrl(URL);
        submenu.getChildren().add(item7);
        model.addSubmenu(submenu);
        
        Submenu submenu2 = new Submenu();
        submenu2.setId(view.createUniqueId());
        submenu.setLabel("Table Type");
        MenuItem item99 = new MenuItem();
        //item2.setId();
        item99.setValue("Add Table Type");
        item99.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderAddTableType.xhtml";
        item99.setUrl(URL);
        submenu2.getChildren().add(item99);
        
         MenuItem item88 = new MenuItem();
        //item2.setId();
        item88.setValue("View All Table Type");
        item88.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewAllTableType.xhtml";
        item88.setUrl(URL);
        submenu.getChildren().add(item88);
        model.addSubmenu(submenu2);
        
        Submenu submenu3 = new Submenu();submenu.setLabel("Reservation");
        submenu3.setId(view.createUniqueId());
        MenuItem item66 = new MenuItem();
        //item2.setId();
        item66.setValue("View All Reservation");
        item66.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderView-SearchAllReservations.xhtml";
        item66.setUrl(URL);
        submenu3.getChildren().add(item66);
        model.addSubmenu(submenu3);
        
        
        MenuItem item77 = new MenuItem();
        //item2.setId();
        item77.setValue("Report Show /No Show");
        item77.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderSelectDateViewReservation.xhtml";
        item66.setUrl(URL);
        submenu3.getChildren().add(item77);
        model.addSubmenu(submenu3);
        
        return model;
       
    }

    //Create Account Management Menu
    public MenuModel generateAccountMenu() {
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        MenuModel model = new DefaultMenuModel();

        Submenu submenu = new Submenu();
        submenu.setId(view.createUniqueId());
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("MY_ACCOUNT_DETAILS"));
        MenuItem item2 = new MenuItem();
        item2.setId(view.createUniqueId());
        item2.setValue("Edit Account Info");
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderEditAccountInfo.xhtml";
        item2.setUrl(URL);
        submenu.getChildren().add(item2);
        model.addSubmenu(submenu);

        Submenu submenu1 = new Submenu();
        submenu1.setId(view.createUniqueId());
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("Service Plan"));
        Submenu submenu2 = new Submenu();
        submenu2.setId(view.createUniqueId());
        submenu2.setLabel("Service");
        MenuItem item3 = new MenuItem();
        item3.setId(view.createUniqueId());
        item3.setValue("Add a Service");
        String url3 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/TCubeAddService.xhtml";
        item3.setUrl(url3);
        MenuItem item4 = new MenuItem();
        item4.setValue("View a service");
        String url4 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/TCubeViewAllService.xhtml";
        item4.setUrl(url4);
        item4.setId(view.createUniqueId());
        submenu.getChildren().add(item4);
        model.addSubmenu(submenu1);

        Submenu submenu4 = new Submenu();
        submenu4.setId(view.createUniqueId());
        submenu4.setLabel("Service Package");
        MenuItem item5 = new MenuItem();
        item5.setId(view.createUniqueId());
        item5.setValue("Add a Package");
        String url5 = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/BranchAddServicePackage.xhtml";
        item5.setUrl(url5);
        model.addSubmenu(submenu4);

        Submenu submenu5 = new Submenu();
        submenu5.setId(view.createUniqueId());
        submenu5.setLabel("Transcations");
        
        MenuItem item7 = new MenuItem();
        item7.setId(view.createUniqueId());
        item7.setValue("View Transactions");
        String url7 = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderChooseViewTransactions.xhtml";
        item7.setUrl(url7);
        submenu5.getChildren().add(item7);
        
        MenuItem item8 = new MenuItem();
        item8.setValue("View bills");
        item8.setId(view.createUniqueId());
        String url8 = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewAllBills.xhtml";
        item8.setUrl(url8);
        submenu5.getChildren().add(item8);
        model.addSubmenu(submenu5);
        
        Submenu submenu6 = new Submenu();
        submenu6.setLabel("Loyalty Program");
        submenu6.setId(view.createUniqueId());
        
        MenuItem item0 = new MenuItem();
        item0.setValue("Apply");
        item0.setId(view.createUniqueId());
        String url18 = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderApplyForULPItem.xhtml";
        item0.setUrl(url18);
        submenu6.getChildren().add(item0);
        
        MenuItem item55 = new MenuItem();
        item55.setValue("Track Item");
        item55.setId(view.createUniqueId());
        String url = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderTrackULPItemApplications.xhtml";
        item55.setUrl(url);
        submenu.getChildren().add(item55);
        model.addSubmenu(submenu6);
        
        return model;

    }

    //Create Advertisement Menu
    public MenuModel generateAdvMenu() {
        MenuModel model = new DefaultMenuModel();
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("ADVERTISEMENT_MANAGEMENT"));
        submenu.setId(view.createUniqueId());

        MenuItem menu6 = new MenuItem();
        menu6.setId(view.createUniqueId());
        menu6.setValue(menuLocalization.getStakeholderMenuItem("ADD_ADVERTISEMENT"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/AddAdvertisement.xhtml";
        menu6.setUrl(URL);
        submenu.getChildren().add(menu6);
        model.addSubmenu(submenu);
        return model;
    }

    //Create Advertisement Menu
    public MenuModel generateMailBoxMenu() {
        MenuModel model = new DefaultMenuModel();
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("MY_MAIL_BOX"));
        submenu.setId(view.createUniqueId());

        MenuItem menu7 = new MenuItem();
        menu7.setId(view.createUniqueId());
        menu7.setValue(menuLocalization.getStakeholderMenuItem("INBOX"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/messaging/ViewAllMessage.xhtml";
        menu7.setUrl(URL);
        submenu.getChildren().add(menu7);
        model.addSubmenu(submenu);

        MenuItem menu8 = new MenuItem();
        menu8.setValue(menuLocalization.getStakeholderMenuItem("TRASH"));
        menu8.setId(view.createUniqueId());
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/messaging/DeleteMessage.xhtml";
        menu8.setUrl(URL);
        submenu.getChildren().add(menu7);
        model.addSubmenu(submenu);
        return model;
    }

    //Create Events Menu
    public MenuModel generateEventMenu() {
        MenuModel model = new DefaultMenuModel();
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("EVENT_MANAGEMENT"));
        submenu.setId(view.createUniqueId());

        MenuItem menu7 = new MenuItem();
        menu7.setId(view.createUniqueId());
        menu7.setValue(menuLocalization.getStakeholderMenuItem("ADD_EVENT"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/common/AddEvent.xhtml";
        menu7.setUrl(URL);
        submenu.getChildren().add(menu7);
        model.addSubmenu(submenu);

        /*
        MenuItem menu8 = new MenuItem();
        menu8.setValue(menuLocalization.getStakeholderMenuItem("MANAGE_EVENT"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/common/ManageEvent.xhtml";
        menu8.setUrl(URL);
        submenu.getChildren().add(menu8);
        model.addSubmenu(submenu);
         */

        MenuItem menu9 = new MenuItem();
        menu9.setId(view.createUniqueId());
        menu9.setValue(menuLocalization.getStakeholderMenuItem("VIEW_ALL_EVENT"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/common/ViewAllEvent.xhtml";
        menu9.setId(view.createUniqueId());
        menu9.setUrl(URL);
        submenu.getChildren().add(menu9);
        model.addSubmenu(submenu);

        return model;
    }

    public MenuModel getMicroPortalModel() {
        return microPortalModel;
    }

    public void setMicroPortalModel(MenuModel microPortalModel) {
        this.microPortalModel = microPortalModel;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }
}
